/**
 * Exit.java<br>
 *
 *
 * @version 上午12:41:04
 */
package com.iwechat.adapter.mina.entity;

import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * 退出请求报文
 *
 *
 */
public class Exit extends PDUHeader {
    public static final int command = PDU.COMMAND_EXIT;
    public static final int pkgLen  = PDU.HEAD_LEN;

    /**
     * 根据header构造Exit for receive
     *
     *
     * @param active
     */
    public Exit(PDUHeader header) {
        this.sequence = header.getSequence();
        this.timestamp = header.getTimestamp();
    }

    /**
     * 构造Exit for send
     *
     *
     * @param active
     */
    public Exit() {
        this.sequence = PDUHeader.getSeqNum();
        this.timestamp = System.currentTimeMillis();
    }

    @Override
    public IoBuffer toIoBuffer(Charset charset) throws Exception {
        IoBuffer buffer = IoBuffer.allocate(HEAD_LEN);
        buffer.putInt(pkgLen);
        buffer.putInt(command);
        buffer.putInt(sequence);
        buffer.putLong(timestamp);
        buffer.flip();
        return buffer;
    }

    @Override
    public String toString() {
        return "Exit [pkgLen=" + pkgLen + ", command=" + command + ", sequence=" + sequence + ", timestamp=" + timestamp + "]";
    }
}
